﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class MachineAdd : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDLType();
            }
            Master.ValidationGroup = "gSubmit";
        
        }

        private void InitDDLType()
        {
            BindParameterForDDL("MachineType", ddlMachineType);
            UIHelper.InsertFirstItem(ddlMachineType);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写机台代码资料！");
                    return;
                }
                if (!string.IsNullOrEmpty(this.txtMachineName.Text.Trim()))
                {
                    if (!ValidateData())
                    {
                         return;
                    }
                    MdMachineEntity entity = new MdMachineEntity();
                    entity.MachineName = GetString(txtMachineName);
                    entity.MachineID = GetString(txtMachineID);
                    entity.StandardTime = GetDecimal(txtStandardTime);

                    entity.MachineType = ddlMachineType.SelectedValue;


                    entity.Status = "1";
                    entity.CreateDate = DateTime.Now;
                    entity.CreateUserID= CurrentUser.UserID;
                    entity.CreateUserName = CurrentUser.UserName;

                    if (MdMachine.InsertMdMachine(entity))
                    {
                        base.LogAction(BaseEnum.ActionType.Add, "", true, null, new EntityBase[] { entity });
                        UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/MachineList.aspx");
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, Message.SaveFail);
                    }
                }

            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }


        #region 数据有效验证
        private bool ValidateData()
        {
            string strMention = "";




            if (string.IsNullOrEmpty(ddlMachineType.SelectedValue))
            {
                strMention += "请选择机台类别！";
            }
            if (string.IsNullOrEmpty(txtMachineName.Text.Trim()))
            {
                strMention += "请输入机台名称！";
            }

            MdMachine entity = new MdMachine();
            //if (entity.IsIDExists(this.lblProductID.Text.Trim()))
            //{
            //    ChangeProductID();
            //    strMention += "产品编号已存在，请重新保存！";

            //}

             if (entity.IsIDExists(this.txtMachineID.Text.Trim()))
            {
                strMention += "机台代码已存在，请重新输入！";

            }
            if (entity.IsNameExists(this.txtMachineName.Text.Trim(),""))
            {
                strMention += "机台名称已存在，请重新输入！";

            }

            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }


        #endregion
    }
}